#include "FormatOutput.h"
#include <Unit.h>

#include <iostream>
using namespace std;

namespace
{

string intToString(int value_i)
{
    ostringstream format; string s;
    format << value_i;
    return (s = format.str());
}

}


void FormatOutput::eachPrint(ostream& os_io, int& value_io,  Unit& unit_i, bool& addNull_io)
{
    int value = value_io / unit_i.getValue();
    if (value > 0)
    {
        if (addNull_io) (os_io << " ");
        os_io << intToString(value) << " " << unit_i.getName();
        addNull_io = true;
    }
    value_io = value_io % unit_i.getValue();
}

void FormatOutput::zeroPrint(ostream& os_io, Unit& unit_i)
{
    os_io << "0 " << unit_i.getName();   
}

void FormatOutput::basePrint(ostream& os_io, const int& value_i, Unit& unit_i)
{
    os_io << intToString(value_i) << " " << unit_i.getName();
}

StdOutput& StdOutput::getStdOutput()
{
    static StdOutput stdOutput;
    return stdOutput;
}

void StdOutput::output(ostream& os_io, const int& baseValue_i, Unit* unit_i)
{
    (0 == baseValue_i) ? unit_i->zeroOutput(os_io, zeroPrint)
                       : unit_i->eachOutput(os_io, baseValue_i, eachPrint);
}


BasicUnitOutput& BasicUnitOutput::getBasicUnitOutput()
{
    static BasicUnitOutput basicOutput;
    return basicOutput;
}

void BasicUnitOutput::output(ostream& os_io, const int& baseValue_i, Unit* unit_i)
{
    (0 == baseValue_i) ? unit_i->zeroOutput(os_io, zeroPrint)
                       : unit_i->baseOutput(os_io, baseValue_i, basePrint);
}



